<template>
  <div id="superUser_add">
    <el-form ref="form" :model="form" label-width="80px" size="mini">
      <el-form-item label="停车场区域" style="width: 200px;margin-right: 100px">
        <el-input v-model="address" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="state" placeholder="请选择状态" style="height: 50px">
          <el-option label="空闲" value="空闲"></el-option>
          <el-option label="占用" value="占用"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="停车场编号">
        <el-input v-model="parkingSpaceNumber"></el-input>
      </el-form-item>
      <el-form-item label="停车场价格">
        <el-input v-model="price"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "superUser_add",
  data() {
    return {}
  },
  props: {
    id: null,
    address: null,
    parkingSpaceNumber: null,
    price: null,
    state: null,
    email: null,
  },
  methods: {
    onSubmit() {
      if (this.state == "空闲") {
        this.state = 0
      } else {
        this.state = 1
      }
      //如果id不为空，代表是更改数据
      if ((typeof this.id) != 'undefined') {
        this.$http.get("http://localhost:8088/superUser/updateParking", {
          params: {
            id: this.id,
            address: this.address,
            parkingSpaceNumber: this.parkingSpaceNumber,
            price: this.price,
            state: this.state,
          }
        }).then(resp => {
          console.log(resp.data)
          if (resp.data) {
            console.log(resp)
            this.$http.get("http://localhost:8088/user/findParkingByAddress", {
              params: {
                address: this.address,
              }
            }).then(resp => {
              this.parking = resp.data
              this.$router.push({
                name: 'superUser_parking',
                query: {
                  parking: this.parking,
                  email: this.user,
                }
              }).catch(err => {
                console.log(err)
              })
            }).catch({})

          } else {
            if (this.state == 0) {
              this.state = '空闲'
            } else {
              this.state = '占用'
            }
            alert("停车位重复了！")
          }
        })
      }else {
        this.$http.get("http://localhost:8088/superUser/addParking", {
          params: {
            address: this.address,
            parkingSpaceNumber: this.parkingSpaceNumber,
            price: this.price,
            state: this.state,
          }
        }).then(resp => {
          console.log(resp.data)
          if (resp.data) {
            console.log(resp)
            this.$http.get("http://localhost:8088/user/findParkingByAddress", {
              params: {
                address: this.address,
              }
            }).then(resp => {
              this.parking = resp.data
              this.$router.push({
                name: 'superUser_parking',
                query: {
                  parking: this.parking,
                  email: this.user,
                }
              }).catch(err => {
                console.log(err)
              })
            }).catch({})

          } else {
            if (this.state == 0) {
              this.state = '空闲'
            } else {
              this.state = '占用'
            }
            alert("停车位重复了！")
          }
        })
      }
      console.log(this.id)
      console.log(this.address)
      console.log(this.parkingSpaceNumber)
      console.log(this.price)
      console.log(this.state)
      console.log(this.email)
      console.log('submit!');
    },
    cancel() {
      if ((typeof this.id)=='undefined'){
        this.$http.get("http://localhost:8088/superUser/findAll", {
        }).then(resp => {
          this.parking = resp.data
          this.$router.push({
            name: 'superUser_parking',
            query: {
              parking: this.parking,
              email: this.user,
            }
          }).catch(err => {
            console.log(err)
          })
        }).catch({})
      }else {
        this.$http.get("http://localhost:8088/user/findParkingByAddress", {
          params: {
            address: this.address,
          }
        }).then(resp => {
          this.parking = resp.data
          this.$router.push({
            name: 'superUser_parking',
            query: {
              parking: this.parking,
              email: this.user,
            }
          }).catch(err => {
            console.log(err)
          })
        }).catch({})
      }
    }
  },
  mounted() {
    if (this.state == 0) {
      this.state = '空闲'
    } else {
      this.state = '占用'
    }
  }
}
</script>

<style scoped>
#superUser_add{
  border: 1px solid beige;
  width: 60%;
  margin: auto;
  margin-top: 100px;
  padding: 20px;
}
label{
  width: 100px;
}
</style>